package com.czl.module_storehouse.activity.receive.change;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ActivityReceiveChangeBinding;
import com.czl.module_storehouse.adapter.ReceiveChangeAdapter;
import com.czl.module_storehouse.event.ReceiveChangeModelEvent;
import com.czl.module_storehouse.event.ReceiveChangeModelPostEvent;
import com.czl.module_storehouse.event.ReceiveChangePostEvent;
import com.czl.module_storehouse.event.ReceiveChangeSortEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * @author xubin
 */
public class ReceiveChangeActivity extends BaseActivity<ActivityReceiveChangeBinding> {

    private ReceiveChangeAdapter mAdapter;
    private SortBean mSortBean;

    @Override
    protected ActivityReceiveChangeBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityReceiveChangeBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("变更物品");
        mAdapter = new ReceiveChangeAdapter(R.layout.item_receive_change_sort, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        binding.textChange.setOnClickListener(v -> nextChangeModelActivity());
        binding.textConfirm.setOnClickListener(v -> submit());
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveChangeSortEvent event) {
        SortBean sortBean = event.getSortBean();
        mSortBean = ParcelHelper.copy(sortBean);
        if (mSortBean == null) {
            return;
        }
        mAdapter.setMaxCount(mSortBean.getCanNum());
        binding.tvTitle.setText(mSortBean.getSortName());
        binding.tvModel.setText(getString(R.string.model_placeholder, mSortBean.getSortModel()));
        binding.tvApplyNum.setText(getString(R.string.apply_num_placeholder, mSortBean.applyNum()));
        binding.textReceiveNum.setText(getString(R.string.receive_num_placeholder, mSortBean.lendNum()));
        binding.textCanNum.setText(getString(R.string.can_num_placeholder, mSortBean.getCanNum()));
        List<SortBean> changeSortList = mSortBean.getChangeSortList();
        if (changeSortList != null) {
            mAdapter.addData(changeSortList);
        }
    }

    /**
     * 返回数据
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveChangeModelPostEvent event) {
        SortBean sortBean = event.getSortBean();
        if (sortBean == null) {
            return;
        }
        mAdapter.addData(sortBean);
    }

    private void nextChangeModelActivity() {
        EventBus.getDefault().postSticky(new ReceiveChangeModelEvent(mSortBean, mAdapter.getData()));
        startActivity(new Intent(getContext(), ReceiveChangeModelActivity.class));
    }

    private void submit() {
        List<SortBean> newList = new ArrayList<>();
        int totalNum = 0;
        for (SortBean sortBean : mAdapter.getData()) {
            sortBean.setChangeReceiveNum(sortBean.getApplyNum());
            if (sortBean.getApplyNum() != null && sortBean.getApplyNum() > 0) {
                sortBean.setChangeSort(true);
                newList.add(sortBean);
                totalNum += sortBean.getApplyNum();
            }
        }
        if (mSortBean != null) {
            mSortBean.setChangeSortList(newList);
//            mSortBean.setApplyNum(mSortBean.getApplyNumInt() - totalNum);
            mSortBean.setChangeReceiveNum(totalNum);
        }
        EventBus.getDefault().postSticky(new ReceiveChangePostEvent(newList, mAdapter.getRemoveSorts(), mSortBean));
        finish();
    }
}
